package com.example.springboot.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.springboot.entity.Like;
import com.example.springboot.entity.dto.LikeDto;
import com.example.springboot.mapper.LikeMapper;
import com.example.springboot.mapper.PostMapper;
import com.example.springboot.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private PostMapper postMapper;
    @Override
    @Transactional
    public void like(LikeDto likeDto, Integer likeId) {
        Like like = BeanUtil.copyProperties(likeDto, Like.class);
        like.setLikeId(likeId);
        if (likeMapper.isEmpty(like) == null) {
            likeMapper.insertLike(like);
            postMapper.increLikes(like.getLikedId());
        } else {
            likeMapper.updateLike(like);
            if(like.getIsLike() == 1){
                postMapper.increLikes(like.getLikedId());
            }else{
                postMapper.decreLikes(like.getLikedId());
            }
        }
    }

    @Override
    public Like getIsLike(Integer likedId, Integer likeId) {
        Like like = new Like();
        like.setLikedId(likedId);
        like.setLikeId(likeId);

        return likeMapper.isEmpty(like);
    }
}
